#include "unit.h"

using namespace GoldenAges;

CUnit::CUnit()
{
}

CUnit::~CUnit()
{
}

void CUnit::Think(const int& iElapsedTime)
{
    CEntity::Think(iElapsedTime);
}

void CUnit::Render(SDL_Surface* pDestSurf)
{
    CEntity::Render(pDestSurf);
}

TiXmlElement CUnit::SaveData(void)
{
    TiXmlElement UnitElm("unit");
	UnitElm.SetAttribute("id", m_iUnitID);
	UnitElm.InsertEndChild(CEntity::SaveData());
	TiXmlElement PositionElm("position");

	char buf[8];

	TiXmlElement PosXElm("x");
	sprintf(buf, "%d", GetTilePosX());
	TiXmlText PosXTxt(buf);
	PosXElm.InsertEndChild(PosXTxt);
	PositionElm.InsertEndChild(PosXElm);

	TiXmlElement PosYElm("y");
	sprintf(buf, "%d", GetTilePosY());
	TiXmlText PosYTxt(buf);
	PosYElm.InsertEndChild(PosYTxt);
	PositionElm.InsertEndChild(PosYElm);

	UnitElm.InsertEndChild(PositionElm);

	return UnitElm;
}

bool CUnit::LoadData(TiXmlElement* pXMLData)
{
    CEntity::LoadData(pXMLData);

	m_iUnitID = atoi(pXMLData->Attribute("player"));

	TiXmlElement* XMLPosition = pXMLData->FirstChildElement("position");
	int iTileX = atoi(XMLPosition->FirstChild( "x" )->FirstChild()->Value()),
        iTileY = atoi(XMLPosition->FirstChild( "y" )->FirstChild()->Value());

	SetTilePos(iTileX, iTileY);

	return true;
}
